package com.ou.csbs.capstone.controllers.menu;

import gov.nasa.worldwind.render.SurfaceShape;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.models.shapes.Text;
import com.ou.csbs.capstone.tools.CircleDrawingTool;
import com.ou.csbs.capstone.tools.EditingTool;
import com.ou.csbs.capstone.tools.FreehandDrawingTool;
import com.ou.csbs.capstone.tools.LineDrawingTool;
import com.ou.csbs.capstone.tools.MeasurementTool;
import com.ou.csbs.capstone.tools.PolyLineDrawingTool;
import com.ou.csbs.capstone.tools.TextDrawingTool;
import com.ou.csbs.capstone.views.menu.ShapeDrawDialog;
import com.ou.csbs.capstone.views.menu.StatusBar;
import com.ou.csbs.capstone.views.menu.TextDrawDialog;
import com.ou.csbs.capstone.views.menu.ToolBar;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;
/**
 * Controller class for all drawing and measurement buttons.
 * 
 * This class also contains the instances of each of the drawing tools, for ease of enabling/disabling
 * 
 * @author Senior Capstone
 */
public class ToolBarController {
	private ToolBar toolbar;
	private RenderableModel model;

	private LineDrawingTool ldt;
	private CircleDrawingTool cdt;
	private PolyLineDrawingTool pldt;
	private FreehandDrawingTool fhdt;
	private TextDrawingTool tdt;
	private EditingTool et;
	private MeasurementTool mt;

	private boolean textDrawingToolActive;

	public ToolBarController(RenderableModel model) {
		setModel(model);
		ldt = new LineDrawingTool(this.model);
		cdt = new CircleDrawingTool(this.model);
		pldt = new PolyLineDrawingTool(this.model);
		fhdt = new FreehandDrawingTool(this.model);
		tdt = new TextDrawingTool(this.model);
		et = new EditingTool(this.model);
		mt = new MeasurementTool(this.model);
	}

	public void setToolBar(ToolBar toolbar){
		this.toolbar = toolbar;
		addActionListener();
	}

	public void setModel(RenderableModel model){
		this.model = model;
	}

	private void removeAllToolListeners(){
		WorldWindCanvas.removeMouseListener(ldt);
		WorldWindCanvas.removeMouseMotionListener(ldt);

		WorldWindCanvas.removeMouseListener(cdt);
		WorldWindCanvas.removeMouseMotionListener(cdt);

		WorldWindCanvas.removeMouseListener(pldt);
		WorldWindCanvas.removeMouseMotionListener(pldt);

		WorldWindCanvas.removeMouseListener(fhdt);
		WorldWindCanvas.removeMouseMotionListener(fhdt);

		WorldWindCanvas.removeMouseListener(tdt);
		WorldWindCanvas.removeMouseMotionListener(tdt);

		WorldWindCanvas.removeSelectListener(et);
		WorldWindCanvas.removeMouseListener(et);
		WorldWindCanvas.removeKeyListener(et);

		WorldWindCanvas.removeMouseListener(mt);
		WorldWindCanvas.removeMouseMotionListener(mt);

		textDrawingToolActive = false;
	}

	public void addActionListener() {
		toolbar.getNormal().addActionListener(new buttonListener());
		toolbar.getLine().addActionListener(new buttonListener());
		toolbar.getCircle().addActionListener(new buttonListener());
		toolbar.getPolyLine().addActionListener(new buttonListener());
		toolbar.getFreehand().addActionListener(new buttonListener());
		toolbar.getText().addActionListener(new buttonListener());
		toolbar.getClearObjects().addActionListener(new buttonListener());
		toolbar.getMeasurement().addActionListener(new buttonListener());
		toolbar.getShapeSettings().addActionListener(new buttonListener());
	}

	public void removeArtifacts(){
		et.deselectObject();
		et.removeControlPoints();
		StatusBar.setMeasurementText("");
	}

	private class buttonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == toolbar.getNormal()){
				removeAllToolListeners();
				WorldWindCanvas.addSelectListener(et);
				WorldWindCanvas.addMouseListener(et);
				WorldWindCanvas.addKeyListener(et);
			}
			else if(e.getSource() == toolbar.getLine()){
				removeArtifacts();
				removeAllToolListeners();
				WorldWindCanvas.addMouseMotionListener(ldt);
				WorldWindCanvas.addMouseListener(ldt);
			}
			else if(e.getSource() == toolbar.getCircle()){
				removeArtifacts();
				removeAllToolListeners();
				WorldWindCanvas.addMouseMotionListener(cdt);
				WorldWindCanvas.addMouseListener(cdt);
			}
			else if(e.getSource() == toolbar.getPolyLine()){
				removeArtifacts();
				removeAllToolListeners();
				WorldWindCanvas.addMouseListener(pldt);
				WorldWindCanvas.addMouseMotionListener(pldt);
			}
			else if(e.getSource() == toolbar.getFreehand()){
				removeArtifacts();
				removeAllToolListeners();
				WorldWindCanvas.addMouseListener(fhdt);
				WorldWindCanvas.addMouseMotionListener(fhdt);
			}
			else if(e.getSource() == toolbar.getText()){
				removeArtifacts();
				removeAllToolListeners();
				textDrawingToolActive = true;
				WorldWindCanvas.addMouseListener(tdt);
				WorldWindCanvas.addMouseMotionListener(tdt);
			}
			else if(e.getSource() == toolbar.getClearObjects()){
				model.getDrawingLayer().removeAllRenderables();
			}
			else if(e.getSource() == toolbar.getMeasurement()){
				removeArtifacts();
				removeAllToolListeners();
				WorldWindCanvas.addMouseListener(mt);
				WorldWindCanvas.addMouseMotionListener(mt);
			}
			else if(e.getSource() == toolbar.getShapeSettings()) {
				if(et.isObjectSelected()){
					if(et.getSelectedObject() instanceof Text)
						new TextDrawDialog((Text) et.getSelectedObject());
					else
						new ShapeDrawDialog(((SurfaceShape) et.getSelectedObject()).getAttributes());
				}
				else{
					if(textDrawingToolActive)
						new TextDrawDialog(null);
					else
						new ShapeDrawDialog(null);
				}
			}
		}
	}
}
